package cn.hzy.admin.controller;

import cn.hzy.admin.models.SysUser;
import cn.hzy.admin.services.system.ISysUserService;
import cn.hzy.toolkit.ApiResult;
import cn.hzy.toolkit.annotation.HzyAuthorization;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@HzyAuthorization(true)
public class LoginController extends BaseController {

    private final ISysUserService sysUserService;

    public LoginController(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 登录
     *
     * @return
     */
    @GetMapping("/login")
    public String login() {
        return this.view("login");
    }

    /**
     * 退出登录
     *
     * @return
     */
    @GetMapping("/logOut")
    public String logOut() {
        return this.viewRedirect("login");
    }

    /**
     * 检查账户
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/login/check")
    @ResponseBody
    public ApiResult check(@RequestBody SysUser sysUser) {
        var token = this.sysUserService.check(sysUser);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", sysUser);
        return this.resultOk(map);
    }

}